package com.ma4567.remotecontroller.VideoRequest;

import com.ma4567.remotecontroller.MysqlHelper.TableRequestLog;
import com.ma4567.remotecontroller.RemoteControllerApplication;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.sql.SQLException;

@CrossOrigin
@RestController
public class MouseControl {
    public static Robot robot;

    @RequestMapping(value = "/api/clickMouse", method = RequestMethod.GET)
    public void controlMouse(HttpServletRequest request,
                             @RequestParam("cookie") String Cookie,
                             @RequestParam("key") int key) throws SQLException {
        TableRequestLog.AddLog("Click mouse " + key, Cookie, request.getRemoteAddr());

        if(RemoteControllerApplication.userManager.JudgeCookie(Cookie)){//鉴cookie
            if(RemoteControllerApplication.userManager.GetUserSingleItemPermission(Cookie, "RemoteControl")){//鉴权
                try {
                    if(robot == null) robot = new Robot();
                    switch (key){
                        case 0://左键
                            robot.mousePress(KeyEvent.BUTTON1_DOWN_MASK);		// 模拟按下鼠标左键
                            robot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);
                            break;
                        case 1://右键
                            robot.mousePress(KeyEvent.BUTTON3_DOWN_MASK);
                            robot.mouseRelease(KeyEvent.BUTTON3_DOWN_MASK);
                            break;
                    }
                } catch (AWTException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
